import { reactive } from 'vue'

export function makeReactive(obj: any) {
  if (typeof obj !== 'object' || obj === null) {
    return obj
  }

  if (Array.isArray(obj)) {
    return obj.map((item: any) => makeReactive(item))
  }

  const reactiveObj: any = {}
  for (const key in obj) {
    reactiveObj[key] = makeReactive(obj[key])
  }
  return reactive(reactiveObj)
}

// 下载excel
export function downloadFile(data: any, download: any) {
  const blob = new Blob([data], {
    type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
  })
  // 获取heads中的filename文件名
  const downloadElement = document.createElement('a')
  // 创建下载的链接
  const href = window.URL.createObjectURL(blob)
  downloadElement.href = href
  // 下载后文件名
  downloadElement.download = download
  document.body.appendChild(downloadElement)
  // 点击下载
  downloadElement.click()
  // 下载完成移除元素
  document.body.removeChild(downloadElement)
  // 释放掉blob对象
  window.URL.revokeObjectURL(href)
}
import { getNewToken } from '@/api/login'
import { setToken } from './token'
export function getNewTokenFn() {
  return getNewToken()
    .then((res) => {
      // 重置token成功
      if (res.data.Success) {
        setToken(res.data.Result)
        return res.data.Result.Token
      } else {
        // 清除过期token
        sessionStorage.removeItem('USAdrop_user')
        sessionStorage.removeItem('Authorization')
        //   重置失败返回登录
        window.location.href = '/login'
        return Promise.reject()
      }
    })
    .catch(() => {
      // 清除过期token
      sessionStorage.removeItem('USAdrop_user')
      sessionStorage.removeItem('Authorization')
      //   重置失败返回登录
      window.location.href = '/login'
      return Promise.reject()
    })
}
